/*
 * @Author: 莫莫
 * @Phone: 13450159681
 * @Description: 
 * @Date: 2024-08-08 15:03:49
 * @LastEditTime: 2024-11-01 17:46:44
 */
import { createStore } from 'vuex';
// import { hexMD5 } from "../static/js/md5";s

function getId() {
	return wx.getStorageSync('user').id
}

function token() {
	return wx.getStorageSync('token')
}
export default createStore({
	state: { //存放状态
		url: 'https://sj.gdobee.top/index.php/api/wanlshop/',
		api: {
			
		}
	},
	mutations: {
		tip(state, title, icon = 'none') {
			uni.showToast({
				title,
				icon,
				mask: true
			})
		},
		jump(state, url) {
			uni.navigateTo({
				url
			})
		}
	},
	actions: {
		request(content, data) {
			return new Promise((resolve) => {
				uni.request({
					url: content.state.url + data.url,
					method: data.method || "GET",
					header: {
						// "ucodelosan":token(),
						'Access-Control-Allow-Origin': '*',
						'Accept': 'application/json',
						'Content-Type': data.method == 'GET' ? 'application/json' :
							'application/x-www-form-urlencoded',
						'token': token(),
					},
					data: data.data,
					success: (res) => {
						if (res.data.code == 401) {
							uni.navigateTo({
								url: '/pages/login/login'
							})
							return
						}
						if (res.data.code == 1) {
							resolve(res.data)
						} else {
							this.commit('tip', res.data.msg)
						}
					}
				})
			})
		},
		upload(content, data) {
			console.log(data);
			return new Promise((resolve) => {
				uni.uploadFile({
					url: content.state.url + content.state.api.uploadImages,
					name: "image",
					filePath: data,
					formData: {
						uid: getId()
					},
					success(res) {
						let obj = JSON.parse(res.data)
						if (obj.code == 1) {
							resolve(obj.avatar)
						} else {
							content.commit('tip', '系统错误')
						}
					},
					fail(res) {
						console.log(res);
					}
				})
			})
		}
	}
})